<?php

namespace app\models;
use yii\db\ActiveRecord;
use yii;
use yii\db\Query;
class Mall extends ActiveRecord
{
    public static function tableName()
    {
        return '{{mall}}';
    }
    /*根据ID 获取单店铺*/
    public function getOneById($id)
    {
        $where=array(
            'id'=>$id
        );
        $info=(new Query())
            ->from('mall')
            ->where($where)
            ->one();
        return $info;
    }

    /*根据CODE 获取单店铺*/
    public function getOneByCode($code)
    {
        $where=array(
            'code'=>$code
        );
        $info=(new Query())
            ->from('mall')
            ->where($where)
            ->one();
        return $info;
    }

    /*获取店铺列表*/
    public function getAll($offset=null,$limit=null)
    {
        $offset=is_null($offset)?0:$offset;
        $limit=is_null($limit)?5:$limit;
        $list=(new Query())
            ->from('mall')
            ->where(array('is_del'=>'0'))
            ->orderBy('create_time DESC')
            ->offset($offset)
            ->limit($limit)
            ->all();
        return $list;
    }

    /*获取店铺 计数*/
    public function getCount($where=null)
    {
        if(is_null($where))
        {
            $count=(new Query())
                ->from('mall')
                ->count();
        }
        else
        {
            $count=(new Query())
                ->from('mall')
                ->where($where)
                ->count();
        }

        return $count;

    }

    /*新建店铺*/
    public function createNew($name,$code)
    {
        $in_data=array(
            'name'=>$name,
            'code'=>$code,
            'create_time'=>time(),
        );
        if(Yii::$app->db->createCommand()->insert('mall',$in_data)->execute())
        {
            return true;
        }
    }

    /*新建店铺 附带用户添加*/
    public function createNewAll($mall_name,$mall_code,$user_name,$user_pass)
    {
        $in_data_mall=array(
            'name'=>$mall_name,
            'code'=>$mall_code,
            'create_time'=>time(),
        );
        $in_data_user=array(
            'name'=>$user_name,
            'pass'=>md5($user_pass),
            'type'=>'normal',
            'mall_id'=>0,
            'create_time'=>time(),
        );
        $db = Yii::$app->db;
        $transaction = $db->beginTransaction();
        try {
            $db->createCommand()->insert('mall',$in_data_mall)->execute();
            $db->createCommand()->insert('user',$in_data_user)->execute();
            $transaction->commit();
        } catch(\Exception $e) {
            $transaction->rollBack();
            return false;
        }
        $user_md=new User();
        $new_mall_info=$this->getOneByCode($in_data_mall['code']);
        $new_user_info=$user_md->getOneByName($in_data_user['name']);
        $up_data_user=array(
            'mall_id'=>$new_mall_info['id'],
        );
        $user_md->editUser($new_user_info['id'],$up_data_user);
        return true;

    }

    /*删除店铺*/
    public function delOne($id)
    {
        #删除旗下桌面
        $desk_md=new Desk();
        $desk_md->delOneByMallId($id);
        #删除旗下用户
        $user_md=new User();
        $user_md->delByMallId($id);
        #删除店铺
        $up_data=array(
            'is_del'=>'1',
        );
        $up_where=array(
            'id'=>$id,
        );
        return Yii::$app->db->createCommand()->update('mall',$up_data,$up_where)->execute();
    }

    /*编辑店铺 名称*/
    public function editName($id,$name)
    {
        $up_data=array(
            'name'=>$name,
        );
        $up_where=array(
            'id'=>$id,
        );
        return Yii::$app->db->createCommand()->update('mall',$up_data,$up_where)->execute();
    }


}
